<?php

namespace App\Http\Controllers\Api\Common;

use App\Http\Controllers\Controller;
use App\Models\Admin\Setting;

use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use App\Models\Api\Common\Sms;
use App\Models\Api\Common\SmsLog;
use App\Models\Common;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use Illuminate\Http\Request;

class SmsController extends Controller
{
    public static function createClient($accessKeyId, $accessKeySecret){
        $config = new Config([
            "accessKeyId" => $accessKeyId,
            "accessKeySecret" => $accessKeySecret
        ]);
        $config->endpoint = "dysmsapi.aliyuncs.com";
        return new Dysmsapi($config);
    }

    public static function main($args){
        return true;
        $cache = Setting::getCache();
        $client = self::createClient($cache['sms_ali_id'], $cache['sms_ali_key']);
        $sendSmsRequest = new SendSmsRequest([
            "phoneNumbers" => $args['phone'],
            "signName" => $cache['sms_ali_sign'],
            "templateCode" => $cache['sms_ali_tcode'],
            "templateParam" => '{"code":"'.$args['code'].'"}',
        ]);
        return $client->sendSms($sendSmsRequest);
    }

    static public function ip($ip,$cache){
        $info =  SmsLog::find($ip);
        if($info){
            if(Common::is_today($info->todaytime)){
                if($info['times']<$cache['sms_ali_ipsms']){
                    $info->times=$info->times+1;
                }else{
                    Common::resJson(['code'=>1011,'data'=>'','msg'=>'每天同IP限制'.$cache['sms_ali_ipsms'].'条']);
                }
            }else{
                $info->times=1;
                $info->todaytime = TIMESTAMP;
            }
        }else{
            $info = new SmsLog;
            $info->ip=$ip;
            $info->times=1;
            $info->todaytime = TIMESTAMP;
            $info->createtime=TIMESTAMP;
        }
        if($info->save()){
            return true;
        }else{
            Common::resJson(['code'=>1010,'data'=>'','msg'=>'错误']);
        }
    }

    static public function send(Request $request){
        $get = $request->only('phone','forget');
        if(isset($get['phone']) && Common::is_phone($get['phone'])){
            if(isset($get['forget'])){
                $user = Common\User::where('phone',$get['phone'])->first();
                if(!$user){
                    Common::resJson(['code'=>1005,'data'=>'','msg'=>'手机号码不存在']);
                }
            }
            $ip = $request->getClientIp();
            $cache = Setting::getCache();
            self::ip($ip,$cache);
            $info =  Sms::find($get['phone']);
            if($info){
                if(Common::is_today($info->todaytime)){
                    if($info['times']<$cache['sms_ali_mnum']){
                        $info->times=$info->times+1;
                    }else{
                        Common::resJson(['code'=>1001,'data'=>'','msg'=>'每天同手机号限制'.$cache['sms_ali_mnum'].'条']);
                    }
                }else{
                    $info->times=1;
                    $info->todaytime = TIMESTAMP;
                }
                $info->total=$info->total+1;
            }else{
                $info = new Sms;
                $info->phone=$get['phone'];
                $info->total=1;
                $info->times=1;
                $info->todaytime = TIMESTAMP;
                $info->createtime=TIMESTAMP;
            }
            $info->smscode= mt_rand(1000,9999);
            $info->expiretime= TIMESTAMP+120;
            if($info->save()){
                $arr = ['phone'=>$get['phone'],'code'=>$info->smscode];
                $res = self::main($arr);
                Common::resJson(['code'=>1002,'data'=>$arr,'msg'=>'发送成功']);
            }else{
                Common::resJson(['code'=>1003,'data'=>'','msg'=>'发送失败']);
            }
        }else{
            Common::resJson(['code'=>1004,'data'=>'','msg'=>'手机号格式错误']);
        }
    }

    public function check(Request $request){
        Sms::check($request);

    }


}

